package com.swlz.service.impl;

import java.sql.SQLException;

import com.swlz.dao.ArticleDAO;
import com.swlz.entity.Article;
import com.swlz.exception.CodeException;
import com.swlz.service.ArticleService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 文章
 * ClassName : com.swlz.service.impl.ArticleServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-02 08:23:37
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class ArticleServiceImpl implements ArticleService {
	
	private ArticleDAO articleDAO = new ArticleDAO();

	@Override
	public int add(Article article) throws CodeException {
		try {
			return articleDAO.insertSelective(article);
		} catch (SQLException e) {
			throw new CodeException(500, "添加文章错误",e);
		}
	}

	@Override
	public int update(Article article) throws CodeException {
		try {
			return articleDAO.updateSelective(article);
		} catch (SQLException e) {
			throw new CodeException(501, "修改文章错误",e);
		}
	}

	@Override
	public int delete(Long articleId) throws CodeException {
		try {
			return articleDAO.deleteById(articleId);
		} catch (SQLException e) {
			throw new CodeException(502, "删除文章错误",e);
		}
	}

	@Override
	public Article get(Long articleId) throws CodeException {
		try {
			return articleDAO.selectById(articleId);
		} catch (SQLException e) {
			throw new CodeException(503, "获取文章失败",e);
		}
	}

	@Override
	public PageVO<Article> page(Article article, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = articleDAO.countByModel(article);
			PageVO<Article> page = new PageVO<>(total, current, rowMax);
			return page.setList(articleDAO.selectByModel(article, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			throw new CodeException(504, "获取文章分页数据失败",e);
		}
	}


}
